﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProcessFile
{
    public class ConvertStk4StackupLayerRow
    {
        private string _row;
        private int _lineIndex;
        public bool IsTopLayer { set; get; }
        public bool IsBottomLayer { set; get; }
        public bool IsTopSolder { set; get; }
        public bool IsBottomSolder { set; get; }
        public int DieliectricIndex;
        //public int LayerId { set; get; }
        public string Unit { set; get; }
        public  List<string[]> contentFileStk { set; get; }
        public string[] _subStackId;
        public string[] SubstacksID
        {
            get { return _subStackId; }
            set { _subStackId = value; }
        }
        public string ID
        {
            get { return Common.RandomId; }
        }
        public ConvertStk4StackupLayerRow(string row, int lineIndex)
        {
            _row = row;
            _lineIndex = lineIndex;
        }
        public string[] Item
        {
            get { return _row.Split('^'); }
        }
        public override string ToString()
        {
            if (Common.CheckIsDielectric(_row))
            {
                return DielectricContent;
            }
            else
            {
                if (Item[Common.DescriptionIndex] == Common.Plane)
                {
                    return LayerPlanerContent;
                }
                return LayerSigneContent;
            }
        }
        public  string GetSubStackTurnOnOf()
        {
            string str = "";
           
            for (int i = 0; i < _subStackId.Length; i++)
            {
                string usedbyprims = "FALSE";
                int turnOnOf = 1;
                if (i == 0)
                {
                    turnOnOf = 0;

                }
                else
                {
                    turnOnOf = Common.CheckOnOf(contentFileStk[0], contentFileStk[i], _lineIndex);
                }
                if ((i ==0&&!Common.CheckIsDielectric(_row)&&(!IsBottomLayer)&&(!IsTopLayer)) )
                {
                    usedbyprims = "TRUE";
                }
               
                str += string.Format(@"LAYER_V{0}_{1}_{2}CONTEXT={3}
LAYER_V{0}_{1}_{2}USEDBYPRIMS={4}
", 8, _lineIndex + 2, _subStackId[i], turnOnOf, usedbyprims);
            }

            return str;
        }
       
        public string DielectricContent
        {
            get
            {
                int type = 1;
                int layerId = 0;
                if (IsTopSolder)
                {
                    layerId = AD14LayerId.LAYER_V8_Top_Solder;
                }
                else if (IsBottomSolder)
                {
                    layerId = AD14LayerId.LAYER_V8_Bottom_Solder;
                }
                else
                {
                    if (_lineIndex >= 2)
                    {
                        layerId = AD14LayerId.LAYER_V8_LayerUser[_lineIndex - 2];
                    }

                }
                switch (Item[Common.DescriptionIndex])
                {
                    case Common.Core:
                        type = 1;
                        break;
                    case Common.Prepreg:
                        type = 2;
                        break;
                    default:
                        type = 3;
                        break;
                }

                string format = string.Format(@"LAYER_V{1}_{2}LAYERID={0}
LAYER_V{1}_{2}ID={3}
LAYER_V{1}_{2}NAME={4}
LAYER_V{1}_{2}USEDBYPRIMS=FALSE
{5}LAYER_V{1}_{2}DIELTYPE={6}
LAYER_V{1}_{2}DIELCONST={7}
LAYER_V{1}_{2}DIELHEIGHT={8}{10}
LAYER_V{1}_{2}DIELMATERIAL={9}
", layerId, 8, _lineIndex + 2, ID, LayerName, GetSubStackTurnOnOf(), type, Item[Common.DielectricConstantIndex], Item[Common.DielectricThinknessIndex],(_subStackId.Length>1&&!(IsTopSolder|IsBottomSolder))?"":Item[Common.MaterialTypeIndex], Unit);
                return format;
            }
        }
        public string LayerSigneContent
        {
            get
            {
                int layerId = 0;
                if (IsTopLayer)
                {
                    layerId = AD14LayerId.LAYER_V8_TopLayer;
                }
                else if (IsBottomLayer)
                {
                    layerId = AD14LayerId.LAYER_V8_Bottom_Layer;
                }
                else
                {
                    if (_lineIndex >= 2)
                    {
                        layerId = AD14LayerId.LAYER_V8_LayerUser[_lineIndex - 2];
                    }

                }
                int componentPlacement = 0;
                if (IsTopLayer)
                {
                    componentPlacement = 1;
                }
                if (IsBottomLayer)
                {
                    componentPlacement = 2;
                }
                string format = string.Format(@"LAYER_V{1}_{2}LAYERID={0}
LAYER_V{1}_{2}ID={3}
LAYER_V{1}_{2}NAME={4}
LAYER_V{1}_{2}USEDBYPRIMS=TRUE
{5}LAYER_V{1}_{2}COPTHICK={6}{8}
LAYER_V{1}_{2}COMPONENTPLACEMENT={7}
", layerId, 8, _lineIndex + 2, ID, LayerName, GetSubStackTurnOnOf(), Item[Common.CopperThicknessIndex], componentPlacement, Unit);
                return format;
            }
        }
        public string LayerPlanerContent
        {
            get
            {
                int layerId = 0;
                if (IsTopLayer)
                {
                    layerId = AD14LayerId.LAYER_V8_TopLayer;
                }
                else if (IsBottomLayer)
                {
                    layerId = AD14LayerId.LAYER_V8_Bottom_Layer;
                }
                else
                {
                    if (_lineIndex >= 2)
                    {
                        layerId = AD14LayerId.LAYER_V8_LayerUser[_lineIndex - 2];
                    }

                }
                if (IsBottomLayer)
                {
                    return LayerSigneContent;
                }
                string format = string.Format(@"LAYER_V{1}_{2}LAYERID={0}
LAYER_V{1}_{2}ID={3}
LAYER_V{1}_{2}NAME={4}
LAYER_V{1}_{2}USEDBYPRIMS=TRUE
{5}LAYER_V{1}_{2}COPTHICK={6}{7}
LAYER_V{1}_{2}PULLBACKDISTANCE=20mil
", layerId, 8, _lineIndex + 2, ID, LayerName, GetSubStackTurnOnOf(), Item[Common.CopperThicknessIndex], Unit);
                return format;
            }
        }
        public string LayerName
        {
            get
            {
                if (IsTopSolder)
                {
                    return "Top Solder";
                }
                if (IsBottomSolder)
                {
                    return "Bottom Solder";
                }
                string layerName = Item[Common.LayerNameIndex];
                if (string.IsNullOrEmpty(layerName))
                {
                    if (DieliectricIndex > 0)
                    {
                        return string.Format("Dielectric{0}", DieliectricIndex);
                    }
                }
                return layerName;
            }
        }
    }
}
